package com.dome.mvp.base;

import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.dome.mvp.R;

/**
 * Time: 6/1/2021 8:52
 * Author: TAO
 * Description:
 */
public class BaseViewHolder<VDB extends ViewDataBinding> extends RecyclerView.ViewHolder {
    private final VDB binding;
    private SparseArray<View> views;

    public BaseViewHolder(@NonNull VDB itemView) {
        super(itemView.getRoot());
        this.binding = itemView;
        views = new SparseArray<>();
    }

    public VDB getBinding() {
        return binding;
    }

    public View getRootView() {
        return itemView;
    }

    //
    public <T extends View> T getView(int id) {
        View view = views.get(id);
        if (view == null) {
            view = itemView.findViewById(id);
            views.put(id, view);
        }
        return (T) view;
    }


    public BaseViewHolder<VDB> setImageResource(int id, int resource) {
        ImageView imageView = getView(id);
        imageView.setImageResource(resource);
        return this;
    }

    public BaseViewHolder<VDB> setTextColor(int id, int color) {
        TextView view = getView(id);
        view.setTextColor(color);
        return this;
    }


}
